package org.yxj.changethemeapp.v

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.viewpager.widget.ViewPager
import org.yxj.changethemeapp.LogUtils

/**
 *
 *
 * @author  on 2024/6/25
 */
class MyViewPager : ViewPager {

    var p = 0

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
        addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                p = position
            }
            override fun onPageSelected(position: Int) {
                p = position
            }
            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }


    override fun onTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            when (it.action) {
                MotionEvent.ACTION_DOWN -> {
                    LogUtils.LOG_V("[childCount]-> childCount = $childCount")
                    LogUtils.LOG_V("[childCount]-> (p + 1) = ${p + 1}")
                    if (childCount <= p ) {
                        parent.requestDisallowInterceptTouchEvent(false)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(true)
                    }
                }
                MotionEvent.ACTION_MOVE -> {
                }

                MotionEvent.ACTION_UP -> {
                    parent.requestDisallowInterceptTouchEvent(false)
                }
            }
        }
        return super.onTouchEvent(ev)
    }
}